﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FreeSwitch.EventSocket.CallHandling
{
	class CallCollection : IEnumerable<Call>
	{
		private List<Call> _items = new List<Call>();

		public void Add(Call call)
		{
			_items.Add(call);
		}

		public Call Get(string id)
		{
			return _items.FirstOrDefault(c => c.Id.Equals(id));
		}

		public bool TryGetValue(string id, out Call call)
		{
			call = Get(id);
			return call != null;
		}

		/// <summary>
		/// Returns an enumerator that iterates through the collection.
		/// </summary>
		/// <returns>
		/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
		/// </returns>
		/// <filterpriority>1</filterpriority>
		public IEnumerator<Call> GetEnumerator()
		{
			return _items.GetEnumerator();
		}

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		public void Remove(string id)
		{
			_items.RemoveAll(c => c.Id.Equals(id));
		}
	}
}
